package com.fcc.wfly.printhead.controller;

import com.fcc.wfly.common.domain.ErrorHandleConst;
import com.fcc.wfly.printhead.domain.PrintheadWarrantyStatus;
import com.fcc.wfly.printhead.hal.PrintheadInfoWarrantyStatusJson;
import com.fcc.wfly.printhead.service.PrintheadService;
import com.fcc.wfly.base.controller.RestApiController;
import com.fcc.wfly.printhead.domain.PrintheadEvent;
import com.fcc.wfly.printhead.hal.PrintheadInfoEventJson;
import com.fcc.wfly.printhead.hal.PrintheadInfoJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class GetPrintheadInfoController extends RestApiController<PrintheadInfoJson> {

	private static final String API_NAME = "GetPrintheadInfo";
	private static final String API_PARAM_PN = "PN";
	private static final String API_PARAM_SN = "SN";
	private static final String API_PARAM_STARDATE = "startDate";
	private static final String API_PARAM_ENDDATE = "endDate";

	@Autowired
	private PrintheadService printheadService;

	@RequestMapping(API_NAME)
	public HttpEntity<PrintheadInfoJson> getDeviceUtilization(HttpServletRequest request, HttpServletResponse response) {
		return process(request, response);
	}

	@Override
	protected String getApiName() {
		return API_NAME;
	}

	@Override
	protected String[] getApiParamNames() {
		return new String[] { API_PARAM_PN, API_PARAM_SN, API_PARAM_STARDATE, API_PARAM_ENDDATE };
	}

	@Override
	protected PrintheadInfoJson process(Map<String, String> params) {
		String PN = params.get(API_PARAM_PN);
		String SN = params.get(API_PARAM_SN);
		String startDate = params.get(API_PARAM_STARDATE);
		String endDate = params.get(API_PARAM_ENDDATE);
		ErrorHandleConst.ParamsValidate(PN,SN,startDate,endDate);
		return process(PN, SN, startDate, endDate);
	}

	private PrintheadInfoJson process(String PN, String SN, String startDate, String endDate) {
		PrintheadInfoJson result = new PrintheadInfoJson();
		result.setProduct_Number(PN);
		result.setSerial_Number(SN);
		result.setStart_Date(startDate);
		result.setEnd_Date(endDate);

		List<PrintheadEvent> printheadEventList = printheadService.getPrintheadEventList(PN, SN, //
				startDate.replace("T", " ").replace("Z", ""), endDate.replace("T", " ").replace("Z", ""));

		List<PrintheadInfoEventJson> Print_Head_Event = new ArrayList<PrintheadInfoEventJson>();
		for (PrintheadEvent printheadEvent : printheadEventList) {
			PrintheadInfoEventJson detail = new PrintheadInfoEventJson();
			detail.setPH_SERIAL_NO(printheadEvent.getPhSerialNo());
			detail.setPEN(printheadEvent.getPen());
			detail.setEVENT_TS(printheadEvent.getEventTs());
			detail.setEVENT_TYPE(printheadEvent.getEventType());
			Print_Head_Event.add(detail);
		}
		result.setPrint_Head_Event(Print_Head_Event);

		List<PrintheadWarrantyStatus> printheadWarrantyStatusList = printheadService.getPrintheadWarrantyStatusList(PN, SN, //
				startDate.replace("T", " ").replace("Z", ""), endDate.replace("T", " ").replace("Z", ""));

		List<PrintheadInfoWarrantyStatusJson> Print_Head_Warranty_Status = new ArrayList<PrintheadInfoWarrantyStatusJson>();
		for (PrintheadWarrantyStatus printheadWarrantyStatus : printheadWarrantyStatusList) {
			PrintheadInfoWarrantyStatusJson detail = new PrintheadInfoWarrantyStatusJson();
			detail.setPH_SERIAL_NO(printheadWarrantyStatus.getPhSerialNo());
			detail.setPEN(printheadWarrantyStatus.getPen());
			detail.setSTATUS(printheadWarrantyStatus.getStatus());
			detail.setSTART_TS(printheadWarrantyStatus.getStartTs());
			detail.setEND_TS(printheadWarrantyStatus.getEndTs());
			Print_Head_Warranty_Status.add(detail);
		}
		result.setPrint_Head_Warranty_Status(Print_Head_Warranty_Status);

		return result;
	}

}